package org.jeecg.modules.nikeProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.nikeProject.entity.*;
import org.jeecg.modules.nikeProject.entity.vo.OssFileVO;
import org.jeecg.modules.nikeProject.entity.vo.SportWorkInfoVO;
import org.jeecg.modules.nikeProject.entity.vo.WorkInfoSchoolVO;
import org.jeecg.modules.nikeProject.entity.vo.WorkInfoSupportVO;
import org.jeecg.modules.nikeProject.mapper.*;
import org.jeecg.modules.nikeProject.service.ISportsWorkInfoService;
import org.jeecg.modules.oss.entity.OssFile;
import org.jeecg.modules.oss.mapper.OssFileMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 案例评审-学校体育工作展开情况
 * @Author: jeecg-boot
 * @Date:   2024-12-05
 * @Version: V1.0
 */
@Service
public class SportsWorkInfoServiceImpl extends ServiceImpl<SportsWorkInfoMapper, SportsWorkInfo> implements ISportsWorkInfoService {

    @Autowired
    private SportsProjectMapper sportsProjectMapper;
    @Autowired
    private SportsWorkInfoMapper sportsWorkInfoMapper;
    @Autowired
    private SportsWorkInfoSupportMapper sportsWorkInfoSupportMapper;
    @Autowired
    private SportsWorkInfoSchoolMapper sportsWorkInfoSchoolMapper;
    @Autowired
    private SportsWorkInfoCertifierMapper sportsWorkInfoCertifierMapper;
    @Autowired
    private SportsProjectFileMapper sportsProjectFileMapper;
    @Autowired
    private OssFileMapper ossFileMapper;
    @Autowired
    private SportsProjectFileServiceImpl sportsProjectFileServiceImpl;

    @Override
    @Transactional
    public Result<?> add(SportsWorkInfo sportsWorkInfo, String username) {
        String projectId = sportsWorkInfo.getProjectId();
        if (null == projectId || "".equals(projectId)) {
            return Result.error("未查找到案例信息，请联系管理员处理！");
        }
        SportsProject sportsProject = sportsProjectMapper.selectById(projectId);
        if (CommonConstant.PROJECT_STEP_FINISH.equals(sportsProject.getProjectStep())){
            return Result.error("案例已提交，不允许编辑!");
        }
        //判断是否存在此案例对应的个人工作情况
        SportsWorkInfo workInfo = getSportsWorkInfoByProjectId(projectId);
        if (null != workInfo){
            updateWorkInfo(workInfo, username, sportsProject.getRoleType());
        } else {
            //更新案例表的步骤
            sportsProject.setUpdateTime(new Date());
            sportsProject.setUpdateBy(username);
            sportsProject.setProjectStep(CommonConstant.PROJECT_STEP_WORK_INFO);
            sportsProjectMapper.updateById(sportsProject);
        }

        //新增学校工作开展情况
        sportsWorkInfo.setCreateBy(username);
        sportsWorkInfo.setCreateTime(new Date());
        int i = sportsWorkInfoMapper.insert(sportsWorkInfo);
        if (i == 0) {
            return Result.error("添加失败", projectId);
        }
        return Result.OK("添加成功！");
    }

    @Override
    public Result<?> addByPromotion(SportWorkInfoVO sportWorkInfoVO, String username) {
        String projectId = sportWorkInfoVO.getProjectId();
        if (null == projectId || "".equals(projectId)) {
            return Result.error("未查找到案例信息，请联系管理员处理！");
        }
        SportsProject sportsProject = sportsProjectMapper.selectById(projectId);
        if (CommonConstant.PROJECT_STEP_FINISH.equals(sportsProject.getProjectStep())){
            return Result.error("案例已提交，不允许编辑!");
        }
        //判断是否存在此案例对应的个人工作情况
        SportsWorkInfo sportsWorkInfo = getSportsWorkInfoByProjectId(projectId);
        if (null != sportsWorkInfo){
            updateWorkInfo(sportsWorkInfo, username, CommonConstant.ROLE_TYPE_PA);
        } else {
            //更新案例表的步骤
            sportsProject.setUpdateTime(new Date());
            sportsProject.setUpdateBy(username);
            sportsProject.setProjectStep(CommonConstant.PROJECT_STEP_WORK_INFO);
            sportsProjectMapper.updateById(sportsProject);
        }
        sportsWorkInfo = new SportsWorkInfo();
        sportsWorkInfo.setProjectId(sportWorkInfoVO.getProjectId());
        sportsWorkInfo.setHowToBuildSchoolCulture(sportWorkInfoVO.getHowToBuildSchoolCulture());
        sportsWorkInfo.setSportCourseStage(sportWorkInfoVO.getSportCourseStage());
        sportsWorkInfo.setSportCourseNum(sportWorkInfoVO.getSportCourseNum());
        sportsWorkInfo.setHealthCourseStage(sportWorkInfoVO.getHealthCourseStage());
        sportsWorkInfo.setHealthCourseNum(sportWorkInfoVO.getHealthCourseNum());
        sportsWorkInfo.setOtherCourse(sportWorkInfoVO.getOtherCourse());
        sportsWorkInfo.setOtherCourseNum(sportWorkInfoVO.getOtherCourseNum());
        sportsWorkInfo.setTeachClassNum(sportWorkInfoVO.getTeachClassNum());
        sportsWorkInfo.setTeachClassStuNum(sportWorkInfoVO.getTeachClassStuNum());

        addAll(sportsWorkInfo, sportWorkInfoVO.getInSchoolList(), sportWorkInfoVO.getOutSchoolList(), sportWorkInfoVO.getSupportList(), username);
        return Result.OK("添加成功！");
    }

    @Transactional
    public void addAll(SportsWorkInfo sportsWorkInfo,
                    List<WorkInfoSchoolVO> inSchoolVOList,
                    List<WorkInfoSchoolVO> outSchoolVOList,
                    List<WorkInfoSupportVO> supportList, String username){
        String projectId = sportsWorkInfo.getProjectId();
        sportsWorkInfoMapper.insert(sportsWorkInfo);
        List<SportsWorkInfoCertifier> certifiers = new ArrayList<>();
        List<OssFile> ossFiles = new ArrayList<>();
        List<SportsProjectFile> projectFiles = new ArrayList<>();
        List<SportsWorkInfoSupport> supports = new ArrayList<>();
        List<SportsWorkInfoSchool> schools = new ArrayList<>();
        //校内
        if (null != inSchoolVOList && inSchoolVOList.size() > 0){
            for (WorkInfoSchoolVO inSchoolVO : inSchoolVOList){
                SportsWorkInfoSchool sportsWorkInfoSchool = new SportsWorkInfoSchool();
                BeanUtils.copyProperties(inSchoolVO, sportsWorkInfoSchool);
                String inSchoolId = IdWorker.getIdStr();
                sportsWorkInfoSchool.setId(inSchoolId);
                sportsWorkInfoSchool.setPromotionType("1");
                sportsWorkInfoSchool.setProjectId(projectId);
                sportsWorkInfoSchool.setCreateBy(username);
                schools.add(sportsWorkInfoSchool);
                List<SportsWorkInfoCertifier> inSchoolCertifiers = inSchoolVO.getCertifiers();
                if (null != inSchoolCertifiers && inSchoolCertifiers.size() > 0){
                    for (SportsWorkInfoCertifier inSchoolCertifier : inSchoolCertifiers){
                        SportsWorkInfoCertifier certifier = new SportsWorkInfoCertifier();
                        BeanUtils.copyProperties(inSchoolCertifier, certifier);
                        certifier.setPromotionType("1");
                        certifier.setProjectId(projectId);
                        certifier.setWorkInfoSchoolId(inSchoolId);
                        certifier.setCreateBy(username);
                        certifiers.add(certifier);
                    }
                }
                List<OssFileVO> fileVOS = inSchoolVO.getOssFileVOS();
                if (null != fileVOS && fileVOS.size() > 0){
                    for (OssFileVO ossFileVO : fileVOS){
                        OssFile ossFile = addOssFile(ossFileVO);
                        ossFiles.add(ossFile);

                        SportsProjectFile sportsProjectFile = new SportsProjectFile();
                        sportsProjectFile.setProjectId(projectId);
                        sportsProjectFile.setWorkInfoSchoolId(inSchoolId);
                        sportsProjectFile.setSourceType("1");
                        sportsProjectFile.setOssFileId(ossFile.getId());
                        sportsProjectFile.setCreateBy(username);
                        projectFiles.add(sportsProjectFile);
                    }
                }
            }
        }

        //校外
        if (null != outSchoolVOList && outSchoolVOList.size() > 0){
            for (WorkInfoSchoolVO outSchoolVO : outSchoolVOList){
                SportsWorkInfoSchool sportsWorkInfoSchool = new SportsWorkInfoSchool();
                BeanUtils.copyProperties(outSchoolVO, sportsWorkInfoSchool);
                String outSchoolId = IdWorker.getIdStr();
                sportsWorkInfoSchool.setId(outSchoolId);
                sportsWorkInfoSchool.setPromotionType("2");
                sportsWorkInfoSchool.setProjectId(projectId);
                sportsWorkInfoSchool.setCreateBy(username);
                schools.add(sportsWorkInfoSchool);
                List<SportsWorkInfoCertifier> inSchoolCertifiers = outSchoolVO.getCertifiers();
                if (null != inSchoolCertifiers && inSchoolCertifiers.size() > 0){
                    for (SportsWorkInfoCertifier inSchoolCertifier : inSchoolCertifiers){
                        SportsWorkInfoCertifier certifier = new SportsWorkInfoCertifier();
                        BeanUtils.copyProperties(inSchoolCertifier, certifier);
                        certifier.setPromotionType("2");
                        certifier.setProjectId(projectId);
                        certifier.setWorkInfoSchoolId(outSchoolId);
                        certifier.setId(IdWorker.getIdStr());
                        certifier.setCreateBy(username);
                        certifiers.add(certifier);
                    }
                }
                List<OssFileVO> fileVOS = outSchoolVO.getOssFileVOS();
                if (null != fileVOS && fileVOS.size() > 0){
                    for (OssFileVO ossFileVO : fileVOS){
                        OssFile ossFile = addOssFile(ossFileVO);
                        ossFiles.add(ossFile);

                        SportsProjectFile sportsProjectFile = new SportsProjectFile();
                        sportsProjectFile.setProjectId(projectId);
                        sportsProjectFile.setWorkInfoSchoolId(outSchoolId);
                        sportsProjectFile.setSourceType("2");
                        sportsProjectFile.setOssFileId(ossFile.getId());
                        sportsProjectFile.setCreateBy(username);
                        projectFiles.add(sportsProjectFile);
                    }
                }
            }
        }

        //支持
        if (null != supportList && supportList.size() > 0){
            for (WorkInfoSupportVO support : supportList){
                SportsWorkInfoSupport workInfoSupport = new SportsWorkInfoSupport();
                BeanUtils.copyProperties(support, workInfoSupport);
                workInfoSupport.setProjectId(projectId);
                workInfoSupport.setCreateBy(username);
                supports.add(workInfoSupport);
            }
        }

        if (null != schools && schools.size() > 0) {
            sportsWorkInfoSchoolMapper.batchInsert(schools);
        }
        if (null != supports && supports.size() > 0) {
            sportsWorkInfoSupportMapper.batchInsert(supports);
        }
        if (null != certifiers && certifiers.size() > 0) {
            sportsWorkInfoCertifierMapper.batchInsert(certifiers);
        }
        if (null != projectFiles && projectFiles.size() > 0) {
            sportsProjectFileMapper.batchInsert(projectFiles);
        }
        if (null != ossFiles && ossFiles.size() > 0) {
            ossFileMapper.batchInsert(ossFiles);
        }
    }

    public OssFile addOssFile (OssFileVO ossFileVO){
        OssFile ossFile = new OssFile();
        String ossFileId = IdWorker.getIdStr();
        ossFile.setId(ossFileId);
        ossFile.setUrl(ossFileVO.getUrl());
        ossFile.setFileName(ossFileVO.getFileName());
        return ossFile;
    }

    @Transactional
    public void updateWorkInfo(SportsWorkInfo workInfo, String username, String roleType){
        workInfo.setUpdateBy(username);
        workInfo.setUpdateTime(new Date());
        workInfo.setIzActive("1");
        sportsWorkInfoMapper.updateById(workInfo);

        //查询当前workInfo下是否还有对应的信息
        String projectId = workInfo.getProjectId();
        List<SportsWorkInfoSchool> workInfoSchools = getSportsWorkInfoSchool(projectId);
        List<String> workInfoSchoolIds = new ArrayList<>();
        if (null != workInfoSchools && workInfoSchools.size() > 0){
            sportsWorkInfoSchoolMapper.updateByProjectId(projectId, username);
            workInfoSchoolIds = workInfoSchools.stream().map(SportsWorkInfoSchool::getId).collect(Collectors.toList());
        }
        List<SportsWorkInfoCertifier> workInfoCertifiers = getWorkInfoCertifiers(projectId);
        if (null != workInfoCertifiers && workInfoCertifiers.size() > 0){
            sportsWorkInfoCertifierMapper.updateByProjectId(projectId, username);
        }
        List<SportsWorkInfoSupport> workInfoSupports = getWorkInfoSupports(projectId);
        if (null != workInfoSupports && workInfoSupports.size() > 0){
            sportsWorkInfoSupportMapper.updateByProjectId(projectId, username);
        }
        //当是推广大使的时候需要将工作内容下对应的图片设置为不可用
        if (CommonConstant.ROLE_TYPE_PA.equals(roleType)) {
            if (null != workInfoSchoolIds && workInfoSchoolIds.size() > 0) {
                List<SportsProjectFile> projectFiles = sportsProjectFileServiceImpl.getProjectFiles(projectId, workInfoSchoolIds);
                if (null != projectFiles && projectFiles.size() > 0) {
                    sportsProjectFileServiceImpl.updateFileList(projectFiles, username);
                }
            }
        }
    }


    public List<SportsWorkInfoSchool> getSportsWorkInfoSchool(String projectId){
        LambdaQueryWrapper<SportsWorkInfoSchool> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsWorkInfoSchool::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsWorkInfoSchool::getProjectId, projectId);
        return sportsWorkInfoSchoolMapper.selectList(lambdaQueryWrapper);
    }

    public List<SportsWorkInfoSupport> getWorkInfoSupports(String projectId){
        LambdaQueryWrapper<SportsWorkInfoSupport> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsWorkInfoSupport::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsWorkInfoSupport::getProjectId, projectId);
        return sportsWorkInfoSupportMapper.selectList(lambdaQueryWrapper);
    }

    public List<SportsWorkInfoCertifier> getWorkInfoCertifiers(String projectId) {
        LambdaQueryWrapper<SportsWorkInfoCertifier> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsWorkInfoCertifier::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsWorkInfoCertifier::getProjectId, projectId);
        return sportsWorkInfoCertifierMapper.selectList(lambdaQueryWrapper);
    }

    public SportsWorkInfo getSportsWorkInfoByProjectId(String projectId) {
        LambdaQueryWrapper<SportsWorkInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsWorkInfo::getProjectId, projectId);
        lambdaQueryWrapper.eq(SportsWorkInfo::getIzActive, "0");
        return sportsWorkInfoMapper.selectOne(lambdaQueryWrapper);
    }
}
